package mapserver;

import httpclient.HttpClient;
import httpclient.Localsource;

import java.io.OutputStream;
import java.util.HashMap;

public class GetTile {

	String localsourcefileString ="e:/localsource/";
	public String[] layers_name;
	public String[] styles_name;
	String format;
	int TileMatrix;
	int TileRow;
	int TileCol;
	
	public void handler(HashMap parameterMap,String basepathString,OutputStream writer)throws Exception
	{
		initial(parameterMap);
		String urlString = "";
		switch (layers_name[0]) {
		case "google":
			urlString = getgoogleurl();
			break;
		case"baidu":
			urlString= getbaiduurl();
			break;
		case"local":
			urlString= getlocalurl();
			break;
		case"gaode":
			urlString=getgaodeurl();
			break;
		case"openstreetmap":
			urlString = getopenstreetmap();
			break;
		case"bing":
			urlString = getbingString();
			break;
		default:
			break;
		}
		 writer.write(("Content-Type:"+format+";charset=GBK\r\n").getBytes());
	     writer.write("\r\n".getBytes());
	     if(layers_name[0].equals("local"))
	     {
	    	 new Localsource().getsourcebyurl(urlString, writer);
	     }
	     else {
	    	 new HttpClient().getsourcebyurl(urlString, writer);
		}  
	}
	
	public void initial(HashMap parameterMap)throws Exception
	{		
		
		if(parameterMap.containsKey("LAYER")) layers_name = ((String) parameterMap.get("LAYER")).split(",");
		if(parameterMap.containsKey("STYLE")) styles_name = ((String) parameterMap.get("STYLE")).split(",");
		if(parameterMap.containsKey("FORMAT")) format = (String)parameterMap.get("FORMAT");
		if(parameterMap.containsKey("TILEMATRIX")) TileMatrix=Integer.parseUnsignedInt((String)parameterMap.get("TILEMATRIX"));
		if(parameterMap.containsKey("TILEROW")) TileRow=Integer.parseUnsignedInt((String)parameterMap.get("TILEROW"));
		if(parameterMap.containsKey("TILECOL")) TileCol=Integer.parseUnsignedInt((String)parameterMap.get("TILECOL"));
		//if(parameterMap.containsKey("TARGET")) target=(String)parameterMap.get("TARGET");
	}
	public String getgoogleurl()
	{
		//String mapurlString="http://mts1.google.com/vt/lyrs=m@285000000&hl=zh-CN&src=app&expIds=201527&rlbl=1&x="+TileRow+"&y="+TileCol+"&z="+TileMatrix+"&s=Galileo";
		//String mapurlString = "http://mt2.google.cn/vt/lyrs=m@284000000,highlight:0x35f05296e7142cb9:0xb9625620af0fa98a@1|style:maps&hl=zh-CN&gl=CN&src=app&expIds=201527&rlbl=1&x="+TileRow+"&y="+TileCol+"&z="+TileMatrix+"&s=Galile";
		//int newx = 1;
		//String mapurlString = "http://mt3.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&x="+TileCol+"&y="+TileRow+"&z="+TileMatrix;
		String mapurlString = "http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&x="+TileCol+"&y="+TileRow+"&z="+TileMatrix;
		return mapurlString;
	}
	public String getbaiduurl()
	{
		//String mapurlString = "http://online1.map.bdimg.com/tile/?qt=tile&x="+TileRow+"&y="+TileCol+"&z="+TileMatrix+"&styles=pl&udt=20141219&scaler=1";
		//System.out.println(mapurlString);
		int newx = 1;
		for(int i =0;i<TileMatrix;i++)
		{
			newx*=2;
		}
		newx = newx-1-TileRow;
		String mapurlString = "http://shangetu1.map.bdimg.com/it/u=x="+TileCol+";y="+newx+";z="+TileMatrix+";v=009;type=sate&fm=46&udt=20140506";
		return mapurlString;
	}
	public String getlocalurl()
	{
		String mapurString= localsourcefileString+TileMatrix+","+TileRow+","+TileCol+".png";
		return mapurString;
	}
		
	public String getgaodeurl()
	{
		String mapString = "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x="+TileCol+"&y="+TileRow+"&z="+TileMatrix;
		//String mapString = "http://b.tile.openstreetmap.org/"+TileMatrix+"/"+TileCol+"/"+TileRow+".png";
		return mapString;
	}
	public String getopenstreetmap()
	{
		String mapString = "http://b.tile.openstreetmap.org/"+TileMatrix+"/"+TileCol+"/"+TileRow+".png";
		return mapString;
	}
	
	
	public String getbingString()
	{
		String quadkey = TileXYToQuadKey(TileCol, TileRow, TileMatrix);
		String mapString = "http://ak.dynamic.t0.tiles.virtualearth.net/comp/ch/"+quadkey+"?mkt=en-us&it=G,VE,BX,L,LA&shading=hill&og=67&n=z";
		return mapString;
	}
	
	 public String TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
     {
         StringBuilder quadKey = new StringBuilder();
         for (int i = levelOfDetail; i > 0; i--)
         {
             char digit = '0';
             int mask = 1 << (i - 1);
             if ((tileX & mask) != 0)
             {
                 digit++;
             }
             if ((tileY & mask) != 0)
             {
                 digit++;
                 digit++;
             }
             quadKey.append(digit);
         }
         return quadKey.toString();
     }
	
}
